<?php
defined('IN_XLP') or exit('Access Denied');
$Document = array(
    'pageid'   => 'finance-refund_form', //页面标示
    'pagename' => '取消订单', //当前页面名称
    'mycss'    => array(), //加载的css样式表
    'myjs'     => array(), //加载的js脚本
    'footerjs' => array(),
    'head'     => false, //加载头部文件
);
include getTpl('header', 'public');
?>
<style>
.pt7{padding-top:7px;text-align:left;}
.tc{text-align:center;}
.txl{width:300px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
.table tbody tr{height:30px;}
.table tbody tr td{line-height:30px;}
.table input{height:30px;width:70px;}
.form-block{float:left;list-style:none;}
.form-block li{margin:10px 0;}
.form-block-b{float:left;width:100%;list-style:none;margin:10px;}
.form-block-b li{float:left;clear:left;}
.form-text-b{float:left;text-align:left;padding-left:5px;}
.form-label-b{float:left;width:100px;text-align:right;font-weight:bold;}
.form-label{display:inline-block;width:100px;text-align:right;font-weight:bold;}
.form-text{display:inline-block;width:150px;text-align:left;padding-left:5px;}
.form-check{float:left;margin:15px 10px;}
.clear{clear:both;}
.red{color:red;}
</style>
<form class="form-horizontal" method="post" id="processForm">
    <ul class="form-block">
        <li>
            <div class="form-label">UID:</div>
            <div class="form-text"><?php echo $refund['user_id']; ?></div>
        </li>
        <li>
            <div class="form-label">收件人:</div>
            <div class="form-text"><?php echo $refund['recipient']; ?></div>
        </li>
        <li>
            <div class="form-label">订单号:</div>
            <div class="form-text"><?php echo $refund['order_sn']; ?></div>
        </li>
        <li>
            <div class="form-label">订单总计:</div>
            <div class="form-text">￥<?php echo $order_info['order_amount']; ?></div>
        </li>
    </ul>
    <ul class="form-block">
        <li>
            <div class="form-label">商品总价:</div>
            <div class="form-text">￥<?php echo $order_info['goods_amount']; ?></div>
        </li>
        <li>
            <div class="form-label">运费:</div>
            <div class="form-text">￥<?php echo $order_info['shipping_fee']; ?></div>
        </li>
        <li>
            <div class="form-label">使用结邻币:</div>
            <div class="form-text"><?php echo $order_info['score_used']; ?> 个</div>
        </li>
        <li>
            <div class="form-label">实付:</div>
            <div class="form-text red">￥<?php echo $order_info['actual_pay']; ?></div>
        </li>
    </ul>
    <ul class="form-block">
        <li>
            <div class="form-label">退款申请时间:</div>
            <div class="form-text"><?php echo date('Y-m-d H:i:s', $refund['return_time']); ?></div>
        </li>
        <li>
            <div class="form-label">客服处理时间:</div>
            <div class="form-text">
            <?php if ($refund['service_time']) echo date('Y-m-d H:i:s', $refund['service_time']); ?>
            </div>
        </li>
        <li>
            <div class="form-label">财务处理时间:</div>
            <div class="form-text">
            <?php if ($refund['finance_time']) echo date('Y-m-d H:i:s', $refund['finance_time']); ?>
            </div>
        </li>        
    </ul>
    <div class="clear"></div>
    <ul class="form-block-b">
        <li>
            <div class="form-label-b">退款/退货原因:</div>
            <div class="form-text-b"><?php echo $refund['return_msg']; ?></div>
        </li>
        <li>
            <div class="form-label-b">客服备注:</div>
            <div class="form-text-b">
            <?php if ($refund['service_msg']) echo $refund['service_msg']; ?>
            </div>
        </li>
        <li>
            <div class="form-label-b">财务备注:</div>
            <div class="form-text-b">
            <?php if ($refund['finance_msg']) echo $refund['finance_msg']; ?>
            </div>
        </li>        
    </ul>
    <div style="">
        <table style="width:1000px;margin:0 7px;" class="table table-striped b-t text-small">
            <thead>
                <tr>
                    <th class="tc" width="55">商品ID</th>
                    <th class="tc" width="210">商品</th>
                    <th class="tc" width="50">售价</th>
                    <th class="tc" width="65">购买数量</th>
                    <th class="tc" width="80">获得结邻币</th>
                    <th class="tc" width="90">申请退款金额</th>
                    <th class="tc" width="90">申请退货数量</th>
                    <th class="tc" width="90">实际退款金额</th>
                    <th class="tc" width="90">实际退货数量</th>
                    <th class="tc" width="90">扣除结邻币</th>
                </tr>
            </thead>
            <tbody>
                <?php $refund_total = 0; $score_minus_total = 0; ?>
                <?php foreach ($order_goods as $k => $v): ?>
                <tr>
                    <td><?php echo $v['goods_id']; ?></td>
                    <td style="text-align:left;">
                        <div class="txl"><?php echo $v['goods_name']; ?></div>
                    </td>
                    <td>￥<?php echo $v['goods_price']; ?></td>
                    <td><?php echo $v['goods_num']; ?></td>
                    <td><?php echo $v['score']; ?></td>
                    <td>￥<?php echo $v['goods_refund']; ?></td>
                    <td><?php echo $v['goods_returnnum']; ?></td>
                    <td>￥<input class="input-sm inputs actual_refund" type="text" name="actual_refund[<?php echo $v['id']; ?>]" value="<?php echo $v['goods_refund']; ?>"></td>
                    <td><input class="input-sm inputs" type="text" name="actual_return[<?php echo $v['id']; ?>]" <?php if($v['goods_returnnum']==0) echo 'readonly style="background:#ccc"'; ?> value="<?php echo $v['goods_returnnum']; ?>"></td>
                    <td><input class="input-sm inputs score_minus" type="text" name="score_minus[<?php echo $v['id']; ?>]" value="<?php echo $v['score_minus']; ?>"></td>
                </tr>
                <?php 
                    $refund_total += $v['goods_refund'];
                    $score_minus_total += $v['score_minus'];
                ?>
                <?php endforeach ?>
            </tbody>
        </table>
        <div class="form-check">
            <input id="return_shipping_fee" type="checkbox" name="return_shipping_fee" value="1" checked>
            <input id="shipping_fee" type="hidden" value="<?php echo $order_info['shipping_fee']; ?>">
            <label for="return_shipping_fee" style="cursor:pointer;">退还运费￥<?php echo $order_info['shipping_fee']; ?></label>
        </div>
        <div class="clear"></div>
        <div class="form-check">
            退款明细：
            退还买家 <span class="red" id="refund_total">￥<?php echo priceFormat($refund_total+$order_info['shipping_fee']); ?></span> 元，
            返还结邻币 <span class="red" id="score_return"><?php echo $refund_coin; ?></span> 个，
            扣除商品奖励结邻币 <span class="red" id="score_minus_total"><?php echo $score_minus_total; ?></span> 个
        </div>
        <div class="clear"></div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">财务备注</label>
        <div class="col-sm-5 pt7">
            <textarea name="note" id="note" cols="30" rows="2" class="bg-focus form-control"></textarea>
            <div class="line line-dashed m-t-large"></div>
        </div>
    </div>
    <?php if ($refund['refund_state'] ==2): //客服同意退款 ?>
    <div class="form-group">
        <div class="col-sm-5 col-lg-offset-3">
            <input type="hidden" name="order_id" value="<?php echo $refund['order_id']; ?>">
            <div class="col-sm-1"><button type="submit" id="sub-ok" data-loading-text="正在提交..." class="btn btn-primary">保存</button></div>
            <button type="button" class="btn btn-white col-md-offset-2" onclick="self.location.reload();">取消</button>
        </div>
    </div>
    <?php endif ?>
</form>
<script>
$(function(){
    $('#processForm').submit(function(){

        if(!confirm('确定要保存吗？')) {
            resetSubmit("#sub-ok",'提交');
            return false;
        }

        $.each($('.inputs'), function(k, v){
            var num = $(v).val();
            if(isNaN(num) || num==='') {
                $(v).css({"background":"yellow"});
                Msg.error('数据格式错误');
                resetSubmit("#sub-ok",'提交');
                return false;
            }
        });

        // Msg.loading();
        $.post('<?php echo U("content/finance/refund_submit");?>',$(this).serialize(),function(result){
            Msg.hide();
            resetSubmit("#sub-ok",'提交');
            if(result.status==1){
                Msg.ok(result.info);
                self.location.reload();
            }else{
                Msg.error(result.info);
            }
        },'JSON');
        return false;
    });

    //退款金额
    var score_used   = <?php echo $order_info['score_used']; ?>;    //使用结邻币数量
    var goods_amount = <?php echo $order_info['goods_amount']; ?>;  //商品总金额
    $('.actual_refund').bind('input propertychange', function() {
        var total_refund = calc('actual_refund', this);
        if(total_refund !== false) {
            $(this).css({"background":"#FFF"});
            //按退款比例计算结邻币数量
            var score_return = (total_refund / goods_amount) * score_used;
            $('#score_return').text(Math.floor(score_return));
            //加邮费
            if($('#return_shipping_fee').is(':checked')) {
                total_refund += parseFloat($('#shipping_fee').val());
            }
            $('#refund_total').text('￥'+total_refund.toFixed(2));
        } else {
            $(this).css({"background":"yellow"});
        }
    });
    //扣除购买商品奖励结邻币数量
    $('.score_minus').bind('input propertychange', function() {
        var total_score_minus = calc('score_minus', this);
        if(total_score_minus !== false) {
            $(this).css({"background":"#FFF"});
            $('#score_minus_total').text(parseInt(total_score_minus));
        } else {
            $(this).css({"background":"yellow"});
        }
    });
    //邮费
    $('#return_shipping_fee').change(function(){
        var fee = parseFloat($('#shipping_fee').val());
        var total = parseFloat($('#refund_total').text().substring(1));
        if($(this).is(':checked')) {
            total += fee;
        } else {
            total -= fee;
        }
        total = total.toFixed(2);
        $('#refund_total').text('￥'+total);
    });
});

function calc(classname, t) {
    var t = $(t).val();
    if( isNaN(t) || t==='') {
        return false;
    }
    var total = 0;
    $.each($('.'+classname), function(k,v){
        total += parseFloat($(v).val());
    });
    return total;
}
</script>
</body>
</html>